import java.util.Arrays;

/**
 * 1109. 航班预订统计
 * https://leetcode-cn.com/problems/corporate-flight-bookings/
 */
public class Solutions_1109 {
    public static void main(String[] args) {
//        int[][] bookings = {{1, 2, 10}, {2, 3, 20}, {2, 5, 25}};
//        int n = 5;  // output: {10, 55, 45, 25, 25}

        int[][] bookings = {{1, 2, 10}, {2, 2, 15}};
        int n = 2;  // output: {10, 25}

        int[] result = corpFlightBookings(bookings, n);
        System.out.println(Arrays.toString(result));
    }

    /**
     * 解题思路：差分数组
     */
    public static int[] corpFlightBookings(int[][] bookings, int n) {
        int[] arr = new int[n + 1];
        for (int[] booking : bookings) {
            int start = booking[0] - 1, end = booking[1] - 1;
            arr[start] += booking[2];
            arr[end + 1] -= booking[2];
        }
        int[] res = new int[n];
        int booking = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            booking += arr[i];
            res[i] = booking;
        }
        return res;
    }
}
